<?php

class ProductListing extends TPage
{
	private $_catID = 0;
	private $_subcatID = 0;
	
	public function getCatID()
	{
		return $this->_catID;
	}

	public function setCatID($value)
	{
		$this->_catID = TPropertyValue::ensureInteger($value);
	}
	
	public function getSubCatID()
	{
		return $this->_subcatID;
	}

	public function setSubCatID($value)
	{
		$this->_subcatID = TPropertyValue::ensureInteger($value);
	}
	
	public function onLoad($param)
	{
		parent::onLoad($param);
		//$this->CurrentPage = ($this->Request->contains('p')) ? TPropertyValue::ensureInteger($this->Request['p']) : 1;
		//$this->SortBy = ($this->Request->contains('sb')) ? TPropertyValue::ensureInteger($this->Request['sb']) : 6;
		//$this->SortType = ($this->Request->contains('st')) ? $this->Request['st'] : 'desc';
		$this->CatID = ($this->Request->contains('c')) ? TPropertyValue::ensureInteger($this->Request['c']) : 0;
		$this->SubCatID = ($this->Request->contains('subc')) ? TPropertyValue::ensureInteger($this->Request['subc']) : 0;		
		//$this->SearchText = ($this->Request->contains('q')) ? $this->Request['q'] : '';
		
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | Product Listing";
			$this->populateData();
		}
	}
	
	public function populateData()
	{
        $sqlmap = $this->Application->Modules['sqlmap']->Client;
		$sql = "";
		/*if (strlen($this->SearchText)>0 && $this->SearchText != $this->Master->DEFAULT_SEARCH_TEXT) {
			$this->lblCatPath->Text = 'Search result for "'.$this->SearchText.'"';
			$this->lblCatPath->Visible = true;
			$sql .= " AND (p.product_id LIKE '%".addslashes($this->SearchText)."%' or p.product_name LIKE '%".addslashes($this->SearchText)."%' or p.product_sku LIKE '%".addslashes($this->SearchText)."%')";
		}
		if ($this->BrandID>0) {
			$sql .= "AND p.brand_id = $this->BrandID ";
		}
		*/
		if ($this->SubCatID>0) {
			$sql .= "AND c.cat_id = $this->SubCatID ";
		}
		else if ($this->CatID>0) {
			$sql .= "AND (c.parent_id = $this->CatID or c.cat_id = $this->CatID) ";
		}
		/*if ($this->IsBestSeller) {
			$this->lblCatPath->Text = 'Best Seller Products';
			$this->lblCatPath->Visible = true;
			$sql .= "AND p.product_best_seller = 1 ";
		}
		if ($this->IsNew) {
			$this->lblCatPath->Text = 'New Arrival Products';
			$this->lblCatPath->Visible = true;
			$sql .= "AND p.product_new_arrival = 1 ";
		}
		if ($this->IsPromoted) {
			$sql .= "AND p.discount_id > 0 ";
		}     
		
		$order = (isset($this->Sortable[$this->SortBy])?$this->Sortable[$this->SortBy]:$this->Sortable[1])." ".$this->SortType;
		
		$this->ProductList->VirtualItemCount = count($sqlmap->queryForList("BrowseProduct", array("ADDITIONAL_CONDITION"=>$sql,"ORDER_BY"=>$order)));
        
		$this->MaxPage = ceil($this->ProductList->VirtualItemCount/$this->ProductList->PageSize);
        if ($this->CurrentPage > $this->MaxPage) $this->CurrentPage = $this->MaxPage;
        $limit = $this->ProductList->PageSize;
        $offset = ($this->CurrentPage-1) * $limit;

        if ($offset + $limit > $this->ProductList->VirtualItemCount)
            $limit = $this->ProductList->VirtualItemCount - $offset;
        if ($this->MyFavourite) {
            $items = $sqlmap->queryForList("FavouriteProduct", $this->Application->User->ID,null,$offset,$limit);
        }
        else {
			if ($this->IsNew)
				$order = "p.product_id desc";
			else
				$order = (isset($this->Sortable[$this->SortBy])?$this->Sortable[$this->SortBy]:$this->Sortable[1])." ".$this->SortType;
            $items = $sqlmap->queryForList("BrowseProduct", array("ADDITIONAL_CONDITION"=>$sql,"ORDER_BY"=>$order),null,$offset,$limit);
        }*/
		
		$limit = null;
		$offset = null;
		$order = "p.product_id desc";
		$items = $sqlmap->queryForList("BrowseProduct", array("ADDITIONAL_CONDITION"=>$sql,"ORDER_BY"=>$order),null,$offset,$limit);
		$this->ProductList->DataSource = $items;
		$this->ProductList->dataBind();
		if (count($items) <= 0)
		{
			//$this->Notice->Type = UserNoticeType::Notice;
			//$this->Notice->Text = $this->Application->getModule("message")->translate("ITEM_FOUND",0,"product");
			//$this->mainBox->Visible = false;
		}
	}
}

?>